package com.jerei.web.utils.ui;

import com.alibaba.fastjson.JSONObject;
import com.jerei.utils.UtilsCommon;
import freemarker.core.Environment;
import freemarker.template.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;

/**
 * Created by sunfeihu on 2016/11/15.
 */
public class DataGridTag implements TemplateDirectiveModel {

    @Autowired
    private HttpServletRequest request;

    private JSONObject defaultOptions = null;

    private void setDefault() {
        defaultOptions = new JSONObject();
        defaultOptions.put("singleSelect", false);
        defaultOptions.put("url", "page");
        defaultOptions.put("headerCls", "jr-easyui-header");
        defaultOptions.put("bodyCls", "jr-easyui-body");
        defaultOptions.put("toolbar", "#tbMain");
        defaultOptions.put("rownumbers", true);
        defaultOptions.put("pagination", true);
        defaultOptions.put("method", "post");
        defaultOptions.put("striped", true);
    }

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        this.setDefault();
        Writer out = env.getOut();
        String dataOptions = UtilsCommon.getString(params.get("options"));
        dataOptions = dataOptions.replaceAll("\r\n", "");
        dataOptions = "{" + dataOptions + "}";
        JSONObject obj = JSONObject.parseObject(dataOptions);
        if (null != obj && obj.size() > 0) {
            Object _value = "";
            for (String key : obj.keySet()) {
                _value = obj.get(key);
                //如果是路径，添加虚拟路径
                if (key.equals("url")) {
                    String __value = UtilsCommon.getString(_value);
                    if (__value.startsWith("/")) {
                        _value = request.getContextPath() + __value;
                    }
                }
                defaultOptions.put(key, _value);
            }
        }
        if (null != defaultOptions && defaultOptions.size() > 0) {
            String str = "";
            for (String key : defaultOptions.keySet()) {
                if (defaultOptions.get(key) instanceof String) {
                    if (key.startsWith("on")) {//如果是on开头，认为是事件，不添加单引号
                        str += key + ":" + defaultOptions.get(key) + ",";
                    } else {
                        str += key + ":\"" + defaultOptions.get(key) + "\",";
                    }
                } else {
                    str += key + ":" + defaultOptions.get(key) + ",";
                }
            }
            str = str.substring(0, str.length() - 1);
            env.setLocalVariable("options", new SimpleScalar(str));
        }
        if (body != null) {
            body.render(env.getOut());
        } else {
            throw new RuntimeException("标签内部至少要加一个空格");
        }
    }
}
